package com.dianshang.controller.system;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dianshang.entity.PageResult;
import com.dianshang.pojo.system.LoginLog;
import com.dianshang.service.system.LoginLogService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Reference
    private LoginLogService loginLogService;

    /**
     * 获取当前登录人
     * @return
     */
    @GetMapping("/name")
    public Map name(){
        // 方法一(获取当前登录人):
        // 添加条件
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        Map map = new HashMap();
        map.put("name", name);
        return map;
        // 方法二(获取当前登录人):
        // return SecurityContextHolder.getContext().getAuthentication().getName();
    }

    /**
     * 查询我的登录日志列表
     */
    @GetMapping("/findPageByLogin")
    public PageResult<LoginLog> findPage(Integer page,
                                         Integer size){
        // 添加条件
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        return loginLogService.findPage(name,page,size);
    }

    /**
     * 修改密码
     */
    @GetMapping("/changePwd")
    public String changePwd(String pwd){
        // 添加条件
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        loginLogService.changePwd(name,pwd);
        return "SUCCESS";
    }

}